home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / ctldata.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  11.3 KB  |  402 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFXCTL_CORE3_SEG
  14. #pragma code_seg(AFXCTL_CORE3_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // OCX Support
  26.  
  27. ////////////////////////////////////////////////////////////////////////////
  28. // _AfxRegisterClipFormat - Registers a custom clipboard format, using the
  29. //  string form of a class ID.
  30.  
  31. CLIPFORMAT AFXAPI _AfxRegisterClipFormat(REFCLSID clsid)
  32. {
  33.     TCHAR pszClsid[40];
  34.     wsprintf(pszClsid,
  35.         _T("{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
  36.         clsid.Data1, clsid.Data2, clsid.Data3,
  37.         clsid.Data4[0], clsid.Data4[1], clsid.Data4[2], clsid.Data4[3],
  38.         clsid.Data4[4], clsid.Data4[5], clsid.Data4[6], clsid.Data4[7]);
  39.     return (CLIPFORMAT)RegisterClipboardFormat(pszClsid);
  40. }
  41.  
  42. CLIPFORMAT AFXAPI _AfxGetClipboardFormatConvertVBX()
  43. {
  44.     static CLIPFORMAT _cfConvertVBX;
  45.     if (_cfConvertVBX == 0)
  46.         _cfConvertVBX = _AfxRegisterClipFormat(CLSID_ConvertVBX);
  47.     ASSERT(_cfConvertVBX != NULL);
  48.     return _cfConvertVBX;
  49. }
  50.  
  51. CLIPFORMAT AFXAPI _AfxGetClipboardFormatPersistPropset()
  52. {
  53.     static CLIPFORMAT _cfPersistPropset;
  54.     if (_cfPersistPropset == 0)
  55.         _cfPersistPropset = _AfxRegisterClipFormat(CLSID_PersistPropset);
  56.     ASSERT(_cfPersistPropset != NULL);
  57.     return _cfPersistPropset;
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // AfxOleMatchPropsetClipFormat
  62.  
  63. BOOL AFXAPI _AfxOleMatchPropsetClipFormat(CLIPFORMAT cfFormat, LPCLSID lpFmtID)
  64. {
  65.     if (cfFormat == _AfxGetClipboardFormatPersistPropset())
  66.     {
  67.         *lpFmtID = CLSID_PersistPropset;
  68.         return TRUE;
  69.     }
  70.  
  71.     if (cfFormat == _AfxGetClipboardFormatConvertVBX())
  72.     {
  73.         *lpFmtID = CLSID_ConvertVBX;
  74.         return TRUE;
  75.     }
  76.  
  77.     return FALSE;
  78. }
  79.  
  80. #if !defined(_WIN32_WCE)
  81. /////////////////////////////////////////////////////////////////////////////
  82. // COleControl::XDataObject
  83.  
  84. STDMETHODIMP_(ULONG) COleControl::XDataObject::AddRef()
  85. {
  86.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  87.     return (ULONG)pThis->ExternalAddRef();
  88. }
  89.  
  90. STDMETHODIMP_(ULONG) COleControl::XDataObject::Release()
  91. {
  92.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  93.     return (ULONG)pThis->ExternalRelease();
  94. }
  95.  
  96. STDMETHODIMP COleControl::XDataObject::QueryInterface(
  97.     REFIID iid, LPVOID* ppvObj)
  98. {
  99.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  100.     return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  101. }
  102.  
  103. STDMETHODIMP COleControl::XDataObject::GetData(LPFORMATETC pformatetcIn,
  104.     LPSTGMEDIUM pmedium )
  105. {
  106.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  107.  
  108.     CControlDataSource* pDataSource = pThis->GetDataSource();
  109.     if (pDataSource == NULL)
  110.         return E_OUTOFMEMORY;
  111.  
  112.     return pDataSource->m_xDataObject.GetData(pformatetcIn, pmedium);
  113. }
  114.  
  115. STDMETHODIMP COleControl::XDataObject::GetDataHere(LPFORMATETC pformatetc,
  116.     LPSTGMEDIUM pmedium )
  117. {
  118.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  119.  
  120.     CControlDataSource* pDataSource = pThis->GetDataSource();
  121.     if (pDataSource == NULL)
  122.         return E_OUTOFMEMORY;
  123.  
  124.     return pDataSource->m_xDataObject.GetDataHere(pformatetc, pmedium);
  125. }
  126.  
  127. STDMETHODIMP COleControl::XDataObject::QueryGetData(LPFORMATETC pformatetc )
  128. {
  129.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  130.  
  131.     CControlDataSource* pDataSource = pThis->GetDataSource();
  132.     if (pDataSource == NULL)
  133.         return E_OUTOFMEMORY;
  134.  
  135.     return pDataSource->m_xDataObject.QueryGetData(pformatetc);
  136. }
  137.  
  138. STDMETHODIMP COleControl::XDataObject::GetCanonicalFormatEtc(
  139.     LPFORMATETC pformatetc, LPFORMATETC pformatetcOut)
  140. {
  141.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  142.  
  143.     CControlDataSource* pDataSource = pThis->GetDataSource();
  144.     if (pDataSource == NULL)
  145.         return E_OUTOFMEMORY;
  146.  
  147.     return pDataSource->m_xDataObject.GetCanonicalFormatEtc(pformatetc,
  148.         pformatetcOut);
  149. }
  150.  
  151. STDMETHODIMP COleControl::XDataObject::SetData(LPFORMATETC pformatetc,
  152.     STGMEDIUM * pmedium, BOOL fRelease)
  153. {
  154.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  155.  
  156.     CControlDataSource* pDataSource = pThis->GetDataSource();
  157.     if (pDataSource == NULL)
  158.         return E_OUTOFMEMORY;
  159.  
  160.     return pDataSource->m_xDataObject.SetData(pformatetc, pmedium, fRelease);
  161. }
  162.  
  163. STDMETHODIMP COleControl::XDataObject::EnumFormatEtc(DWORD dwDirection,
  164.     LPENUMFORMATETC* ppenumFormatEtc)
  165. {
  166.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  167.  
  168.     CControlDataSource* pDataSource = pThis->GetDataSource();
  169.     if (pDataSource == NULL)
  170.         return E_OUTOFMEMORY;
  171.  
  172.     return pDataSource->m_xDataObject.EnumFormatEtc(dwDirection,
  173.         ppenumFormatEtc);
  174. }
  175.  
  176. STDMETHODIMP COleControl::XDataObject::DAdvise(FORMATETC* pFormatetc,
  177.     DWORD advf, LPADVISESINK pAdvSink, DWORD* pdwConnection)
  178. {
  179.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  180.  
  181.     *pdwConnection = 0;
  182.  
  183.     if (pThis->m_pDataAdviseHolder == NULL &&
  184.         CreateDataAdviseHolder(&pThis->m_pDataAdviseHolder) != S_OK)
  185.     {
  186.         return E_OUTOFMEMORY;
  187.     }
  188.     ASSERT(pThis->m_pDataAdviseHolder != NULL);
  189.     return pThis->m_pDataAdviseHolder->Advise(this, pFormatetc, advf, pAdvSink,
  190.         pdwConnection);
  191. }
  192.  
  193. STDMETHODIMP COleControl::XDataObject::DUnadvise(DWORD dwConnection)
  194. {
  195.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  196.  
  197.     if (pThis->m_pDataAdviseHolder == NULL)
  198.         return E_FAIL;
  199.  
  200.     ASSERT(pThis->m_pDataAdviseHolder != NULL);
  201.     return pThis->m_pDataAdviseHolder->Unadvise(dwConnection);
  202. }
  203.  
  204. STDMETHODIMP COleControl::XDataObject::EnumDAdvise(
  205.     LPENUMSTATDATA* ppenumAdvise)
  206. {
  207.     METHOD_PROLOGUE_EX_(COleControl, DataObject)
  208.  
  209.     *ppenumAdvise = NULL;
  210.  
  211.     if (pThis->m_pDataAdviseHolder == NULL)
  212.         return E_FAIL;
  213.  
  214.     ASSERT(pThis->m_pDataAdviseHolder != NULL);
  215.     return pThis->m_pDataAdviseHolder->EnumAdvise(ppenumAdvise);
  216. }
  217. #endif // _WIN32_WCE
  218.  
  219. /////////////////////////////////////////////////////////////////////////////
  220. // COleControl::CControlDataSource
  221.  
  222. COleControl::CControlDataSource::CControlDataSource(COleControl* pCtrl) :
  223.     m_pCtrl(pCtrl)
  224. {
  225.     m_nGrowBy = 4;  // By default, control puts 4 entries in cache.
  226. }
  227.  
  228. BOOL COleControl::CControlDataSource::OnRenderGlobalData(
  229.     LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  230. {
  231.     ASSERT_VALID(this);
  232.     return m_pCtrl->OnRenderGlobalData(lpFormatEtc, phGlobal);
  233.     // Note: COleDataSource has no implementation
  234. }
  235.  
  236. BOOL COleControl::CControlDataSource::OnRenderFileData(
  237.     LPFORMATETC lpFormatEtc, CFile* pFile)
  238. {
  239.     ASSERT_VALID(this);
  240.     return m_pCtrl->OnRenderFileData(lpFormatEtc, pFile);
  241.     // Note: COleDataSource has no implementation
  242. }
  243.  
  244. BOOL COleControl::CControlDataSource::OnRenderData(
  245.     LPFORMATETC lpFormatEtc, LPSTGMEDIUM lpStgMedium)
  246. {
  247.     ASSERT_VALID(this);
  248.     if (m_pCtrl->OnRenderData(lpFormatEtc, lpStgMedium))
  249.         return TRUE;
  250.  
  251.     return COleDataSource::OnRenderData(lpFormatEtc, lpStgMedium);
  252. }
  253.  
  254. BOOL COleControl::CControlDataSource::OnSetData(LPFORMATETC lpFormatEtc,
  255.     LPSTGMEDIUM lpStgMedium, BOOL bRelease)
  256. {
  257.     ASSERT_VALID(this);
  258.     return m_pCtrl->OnSetData(lpFormatEtc, lpStgMedium, bRelease);
  259.     // Note: COleDataSource has no implementation
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. // COleControl overridables for IDataObject implementation
  264.  
  265. BOOL COleControl::OnRenderGlobalData(LPFORMATETC lpFormatEtc,
  266.     HGLOBAL* phGlobal)
  267. {
  268. #ifndef _DEBUG
  269.     UNUSED(lpFormatEtc); // unused in release builds
  270.     UNUSED(phGlobal);    // unused in release builds
  271. #endif
  272.  
  273.     ASSERT_VALID(this);
  274.     ASSERT(AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
  275.     ASSERT(AfxIsValidAddress(phGlobal, sizeof(HGLOBAL)));
  276.  
  277.     return FALSE;   // default does nothing
  278. }
  279.  
  280. BOOL COleControl::OnRenderFileData(LPFORMATETC lpFormatEtc, CFile* pFile)
  281. {
  282. #ifndef _DEBUG
  283.     UNUSED(lpFormatEtc); // unused in release builds
  284.     UNUSED(pFile);       // unused in release builds
  285. #endif
  286.  
  287.     ASSERT_VALID(this);
  288.     ASSERT(AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC)));
  289.     ASSERT_VALID(pFile);
  290.  
  291.     return FALSE;   // default does nothing
  292. }
  293.  
  294. BOOL COleControl::OnRenderData(
  295.     LPFORMATETC lpFormatEtc, LPSTGMEDIUM lpStgMedium)
  296. {
  297.     ASSERT_VALID(this);
  298.     ASSERT(AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
  299.     ASSERT(AfxIsValidAddress(lpStgMedium, sizeof(STGMEDIUM)));
  300.  
  301.     // default implementation does not support extended layout
  302.     if (lpFormatEtc->lindex != -1)
  303.         return FALSE;
  304.  
  305. #if !defined(_WIN32_WCE)
  306.     // default implementation supports CF_METAFILEPICT
  307.     if (lpFormatEtc->cfFormat == CF_METAFILEPICT)
  308.         return GetMetafileData(lpFormatEtc, lpStgMedium);
  309. #endif // _WIN32_WCE
  310.  
  311.     // default implementation supports propset format
  312.     CLSID fmtid;
  313.     if (_AfxOleMatchPropsetClipFormat(lpFormatEtc->cfFormat, &fmtid))
  314.     {
  315.         return GetPropsetData(lpFormatEtc, lpStgMedium, fmtid);
  316.     }
  317.  
  318.     return FALSE;   // cfFormat not supported
  319. }
  320.  
  321. BOOL COleControl::OnSetData(LPFORMATETC lpFormatEtc, LPSTGMEDIUM lpStgMedium,
  322.     BOOL bRelease)
  323. {
  324.     ASSERT_VALID(this);
  325.     ASSERT(AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE));
  326.     ASSERT(AfxIsValidAddress(lpStgMedium, sizeof(STGMEDIUM)));
  327.  
  328.     // default implementation supports propset format
  329.     BOOL bSuccess = FALSE;
  330.     CLSID fmtid;
  331.     if (_AfxOleMatchPropsetClipFormat(lpFormatEtc->cfFormat, &fmtid))
  332.     {
  333.         bSuccess = SetPropsetData(lpFormatEtc, lpStgMedium, fmtid);
  334.  
  335.         if (bSuccess && bRelease)
  336.             WCE_FCTN(ReleaseStgMedium)(lpStgMedium);
  337.     }
  338.  
  339.     return bSuccess;
  340. }
  341.  
  342. /////////////////////////////////////////////////////////////////////////////
  343. // COleControl::GetDataSource
  344.  
  345. COleControl::CControlDataSource* COleControl::GetDataSource()
  346. {
  347.     TRY
  348.     {
  349.         if (m_pDataSource == NULL)
  350.         {
  351.             AFX_MANAGE_STATE(m_pModuleState);
  352.             m_pDataSource = new CControlDataSource(this);
  353.             ASSERT(m_pDataSource != NULL);
  354.             SetInitialDataFormats();
  355.         }
  356.     }
  357.     END_TRY
  358.  
  359.     return m_pDataSource;
  360. }
  361.  
  362. /////////////////////////////////////////////////////////////////////////////
  363. // COleControl::SetInitialDataFormats
  364.  
  365. void COleControl::SetInitialDataFormats()
  366. {
  367.     // by default a COleControl supports CF_METAFILEPICT
  368.     FORMATETC formatEtc;
  369. #if !defined(_WIN32_WCE)
  370. // WinCE: Windows does not support Metafiles
  371.     formatEtc.cfFormat = CF_METAFILEPICT;
  372.     formatEtc.ptd = NULL;
  373.     formatEtc.dwAspect = DVASPECT_CONTENT;
  374.     formatEtc.lindex = -1;
  375.     formatEtc.tymed = TYMED_MFPICT;
  376.     m_pDataSource->DelayRenderData(0, &formatEtc);
  377. #endif // _WIN32_WCE
  378.  
  379.     // by default a COleControl supports persistent propset format
  380.     // (GetData or SetData)
  381.     formatEtc.cfFormat = _AfxGetClipboardFormatPersistPropset();
  382.     formatEtc.ptd = NULL;
  383.     formatEtc.dwAspect = DVASPECT_CONTENT;
  384.     formatEtc.lindex = -1;
  385.     formatEtc.tymed = TYMED_ISTREAM | TYMED_ISTORAGE;
  386.     m_pDataSource->DelayRenderData(0, &formatEtc);
  387.     m_pDataSource->DelaySetData(0, &formatEtc);
  388.  
  389.     // by default a COleControl supports VBX conversion propset format
  390.     // (SetData only)
  391.     formatEtc.cfFormat = _AfxGetClipboardFormatConvertVBX();
  392.     m_pDataSource->DelaySetData(0, &formatEtc);
  393. }
  394.  
  395.  
  396. /////////////////////////////////////////////////////////////////////////////
  397. // Force any extra compiler-generated code into AFX_INIT_SEG
  398.  
  399. #ifdef AFX_INIT_SEG
  400. #pragma code_seg(AFX_INIT_SEG)
  401. #endif
  402.